package com.xdjmall.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xdjmall.common.Constance;
import com.xdjmall.service.CarouselService;
import com.xdjmall.service.CategoryService;
import com.xdjmall.service.IndexConfigService;
import com.xdjmall.service.impl.CarouselServiceImpl;
import com.xdjmall.service.impl.CategoryServiceImpl;
import com.xdjmall.service.impl.IndexConfigServiceImpl;
import com.xdjmall.servlet.vo.IndexCarouselVO;
import com.xdjmall.servlet.vo.IndexCategoryVO;
import com.xdjmall.servlet.vo.IndexGoodsVO;

@WebServlet("/index/*")
public class IndexServlet extends BaseServlet{

	private CategoryService ics = new CategoryServiceImpl();
	private CarouselService cs = new CarouselServiceImpl();
	private IndexConfigService icser = new IndexConfigServiceImpl();
	
	public void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		HttpSession session = req.getSession();
		try {
			List<IndexCategoryVO> categoriesForIndex = ics.getCategoriesForIndex();
			List<IndexCarouselVO> indexCarousel = cs.getIndexCarousel(5);
			List<IndexGoodsVO> hots = icser.getIndexGoods(Constance.INDEX_TYPE_HOT,Constance.INDEX_HOT_NUM);
			List<IndexGoodsVO> news = icser.getIndexGoods(Constance.INDEX_TYPE_NEW,Constance.INDEX_NEW_NUM);
			List<IndexGoodsVO> recom = icser.getIndexGoods(Constance.INDEX_TYPE_RECOMMAD,Constance.INDEX_RECOMMAD_NUM);
			
			session.setAttribute("categories", categoriesForIndex);
			session.setAttribute("carousels", indexCarousel);
			session.setAttribute("newGoodses", news);
			session.setAttribute("hotGoodse", hots);
			session.setAttribute("recommendGoodses", recom);
			req.getRequestDispatcher("/mall/index.jsp").forward(req, resp);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
